package com.chen.blue.security.service.login;

import cn.hutool.core.util.StrUtil;
import com.chen.blue.domain.model.system.LoginUser;

import com.chen.blue.domain.model.system.UsersModel;

import com.chen.blue.service.system.IUsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * Created By CLT On 2023/10/30.
 */
@SuppressWarnings("all")
@Slf4j
@Component("userDetilService")
public class AuthenticationService implements UserDetailsService {

    /**
     * 自定义认证逻辑(基于数据库)
     */

    @Autowired
    private IUsersService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        if (StrUtil.isNotBlank(username)) {

            UsersModel user = userService.getUserByUsername(username);

            if (Objects.nonNull(user)) {
                LoginUser loginUser = new LoginUser();
                loginUser.setSysUser(user);
                return loginUser;
            }

        }

        return null;
    }

}
